﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using AlexandriaPressAdminBusinessLayer;
using ApplicantObj = AlexandriaPressAdminObject.ApplicantInfo;
using MailSender;
using AlexandriaPressUserBusinessLayer;

namespace AlexandriaPressWebsite.MasterPages
{
    public partial class WebForm10 : System.Web.UI.Page
    {

        protected void Page_Init(object sender, EventArgs e)
        {
            if (HttpContext.Current.User.Identity.Name != "")
            {
                UserBusinsessLayer business = new UserBusinsessLayer();
                string _UserType = business.getUserType(HttpContext.Current.User.Identity.Name);
                if (_UserType != "Type_Admin")
                {
                    Response.Redirect("~/MasterPages/ErrorPage.aspx");
                }
            } 
        }

        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack) BindData();
            
        }

        private void BindData()
        {
            gridApplicationList.DataSource = AuthorApplicationBL.viewApplicants();
            gridApplicationList.EmptyDataText = "No author application";
            gridApplicationList.DataBind();

        }
        protected void btnApprove_Click(object sender, EventArgs e)
        {
            int countChecked = 0;
            List<ApplicantObj> applicants = new List<ApplicantObj>();
            ApplicantObj appObj;
            for (int i = 0; i < gridApplicationList.Rows.Count; i++)
            {
                CheckBox selected = (CheckBox)gridApplicationList.Rows[i].FindControl("cbSelect");
                if (selected.Checked)
                {
                    appObj = new ApplicantObj();
                    appObj.ID = Convert.ToInt32(gridApplicationList.DataKeys[i]["ID"].ToString());
                    appObj.Email = Convert.ToString(gridApplicationList.DataKeys[i]["Email"].ToString());
                    appObj.adminID = Convert.ToInt32(Session["userID"]);
                    EmailNotification.AdminMail("admin@alexandria.com",appObj.Email,null,null,"Author application","you have been approved as an author effective today "+DateTime.Now.ToString());
                    applicants.Add(appObj);
                    countChecked++;
                }
            }
            if (countChecked > 0)
            {
                lblNotification.Visible = true;
                int x = AuthorApplicationBL.approveApplicants(applicants);
                lblNotification.Text = x + " application/s approved";
                BindData();

            }
            else
            {
                lblNotification.Visible = true;
                lblNotification.Text = "No data selected";
            }


        }

        protected void btnDeny_Click1(object sender, EventArgs e)
        {
            int countChecked = 0;
            List<ApplicantObj> applicants = new List<ApplicantObj>();
            ApplicantObj appObj;
            for (int i = 0; i < gridApplicationList.Rows.Count; i++)
            {
                CheckBox selected = (CheckBox)gridApplicationList.Rows[i].FindControl("cbSelect");
                if (selected.Checked)
                {
                    appObj = new ApplicantObj();
                    appObj.ID = Convert.ToInt32(gridApplicationList.DataKeys[i]["ID"].ToString());
                    appObj.Email = Convert.ToString(gridApplicationList.DataKeys[i]["Email"].ToString());
                    appObj.adminID = Convert.ToInt32(Session["userID"]);
                    EmailNotification.AdminMail("admin@alexandria.com", appObj.Email, null, null, "Author application", "your application for author has been denied " + DateTime.Now.ToString());
                    applicants.Add(appObj);
                    countChecked++;
                }
            }

            if (countChecked > 0)
            {
                lblNotification.Visible = true;
                int x = AuthorApplicationBL.denyApplicants(applicants);
                lblNotification.Text = x + " application/s denied";
                BindData();

            }
            else
            {
                lblNotification.Visible = true;
                lblNotification.Text = "No data selected";
            }

        }

        protected void gridApplicationList_OnPageIndexChanging(object sender, GridViewPageEventArgs e)
        {
            gridApplicationList.Visible = true;
            gridApplicationList.PageIndex = e.NewPageIndex;
            gridApplicationList.DataSource = AuthorApplicationBL.viewApplicants();
            gridApplicationList.DataBind();
        }
    }
}
